// 封装分类数据相关的代码
import { ref, onMounted } from 'vue'
import { getCategoryAPI } from '@/apis/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

export function useCategory() {
  const route = useRoute()
  const category = ref({})
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryAPI(id)
    category.value = res.result
  }
  onMounted(() => getCategory())
  // 目标：路由参数变化的时候，分类接口可以重新发送
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id)
  })

  return {
    category,
  }
}
